import { ElMessage } from "element-plus";
import Cookies from "js-cookie";
import { createRouter, createWebHistory } from "vue-router";
import { post } from "../network/index";
const routes = [
  {
    meta: {
      title: "欢迎登录",
    },
    name: "login",
    alias: "/sign",
    path: "/login",
    component: () => import("../pages/login/login.vue"),
  },
  {
    meta: {
      title: "首页",
    },
    name: "index",
    auth: true,
    path: "/",
    alias: "/index",
    component: () => import("../pages/index.vue"),
  },
  {
    meta: {
      title: "用户",
    },
    path: "/user",
    name: "user",
    auth: true,
    alias: "/sb",
    component: () => import("../pages/user/user.vue"),
  },
  {
    meta: {
      title: "滚动加载",
    },
    path: "/about",
    name: "about",
    alias: "/about",
    auth: true,
    component: () => import("../pages/about/about.vue"),
  },
  {
    meta: {
      title: "表单组件",
      auth: true,
    },
    path: "/form",
    alias: "/table",
    component: () => import("../pages/index.vue"),
    children: [
      {
        meta: {
          title: "表单",
        },
        path: "table",
        auth: true,
        component: () => import("../components/table/index.vue"),
      },
    ],
  },
  {
    meta: {
      title: "组件库",
    },
    path: "/tool",
    name: "component",
    auth: true,
    component: () => import("../pages/index.vue"),
    children: [
      {
        meta: {
          title: "轮播图",
        },
        auth: true,
        path: "swiper",
        name: "swiper",
        component: () => import("../components/swiper/slide.vue"),
      },
      {
        meta: {
          title: "视频播放",
        },
        auth: true,
        path: "video",
        name: "video",
        component: () => import("../components/video/video.vue"),
      },
    ],
  },
];
const router = new createRouter({
  history: createWebHistory("/"),
  routes,
});
router.beforeEach((to, from, next) => {
  // from 当前路径 to 前往的路径 next 前往的方法
  let cookie = Cookies.get("token");
  if (cookie === undefined && to.name !== "login") {
    next({ path: "/login" });
  } else {
    post("user/verity").then((e) => {
      console.log("权限", e);
    });
    next();
  }
});

export default router;
